package com.example.loginpilot_01.controller;

import com.example.loginpilot_01.entity.Announcement;
import com.example.loginpilot_01.pojo.dto.ApiResult;
import com.example.loginpilot_01.pojo.dto.NoticeQueryDTO;
import com.example.loginpilot_01.pojo.dto.NoticeStatDTO;
import com.example.loginpilot_01.pojo.dto.PageResult;
//import com.example.loginpilot_01.pojo.entity.Notice;
import com.example.loginpilot_01.service.notice.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

// 通知控制器
@RestController
@RequestMapping("/api/notices")
@Validated
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    //通知列表查询（包含搜索筛选）
    //用于查询通知列表，支持搜索筛选和分页
    @GetMapping
    //@RequestParam注解用于获取请求参数
    //@DateTimeFormat注解用于格式化日期参数
    //@RequestHeader注解用于获取请求头中的用户ID
    //ApiResult.success(PageResult<Notice>)表示成功响应,包含分页的通知列表
    //ApiResult.error("错误消息")表示错误响应,包含错误消息
    public ApiResult<PageResult<Announcement>> getNoticeList(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Integer priority,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestHeader("X-User-Id") Long userId) {

        //构建查询参数对象NoticeQueryDTO
        //用于搜索通知标题或内容中包含指定关键词的通知
        NoticeQueryDTO query = NoticeQueryDTO.builder()
                .keyword(keyword)
                .category(category)
                .priority(priority)
                .startDate(startDate)
                .endDate(endDate)
                .pageNum(page)
                .pageSize(size)
                .build();

        //调用noticeService.getNoticeList方法查询通知列表
        //query表示查询参数对象,userId表示当前用户ID
        PageResult<Announcement> result = noticeService.getAnnouncementList(query, userId);
        return ApiResult.success(result);
    }

    //通知详情查询
    //用于查询指定通知的详细信息
    @GetMapping("/{id}")
    public ApiResult<Announcement> getNoticeDetail(
            //@PathVariable注解用于获取路径参数中的通知ID
            @PathVariable Long id,
            @RequestHeader("X-User-Id") Long userId) {

        Announcement announcement = noticeService.getAnnouncementDetail(id, userId);
        return ApiResult.success(announcement);
    }

    //通知阅读统计查询
    //用于查询指定通知的阅读统计信息
    @GetMapping("/{id}/statistics")
    public ApiResult<NoticeStatDTO> getNoticeStatistics(@PathVariable Long id) {
        NoticeStatDTO stat = noticeService.getAnnouncementStatistics(id);
        return ApiResult.success(stat);
    }
}